package socilgirl.dell.mydemo.httpmanager.callback;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import socilgirl.dell.mydemo.httpmanager.IView;

/**
 * Created by ${小强} on 2017/10/26.
 */

public abstract class ViewCallback<T> implements IRequestCallback<T> {
    private Type genericityType;
    Reference<IView> mIViewReference;

    public ViewCallback(IView iView) {
        mIViewReference = new WeakReference<>(iView);
        Type genericSuperclass = getClass().getGenericSuperclass();
        if (genericSuperclass instanceof ParameterizedType) {
            this.genericityType = ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
        } else {
            this.genericityType = Object.class;
        }
    }

    @Override
    public Type getGenericityType() {
        return genericityType;
    }


    @Override
    public void onStart() {
        if (mIViewReference.get() != null) {
            mIViewReference.get().showLoadView();
        }
    }

    @Override
    public void onSuccess(T t, boolean isFromCache) {
        if (mIViewReference.get() != null) {
            mIViewReference.get().showContentView();
        }
    }

    @Override
    public void onError(int code, String msg, Exception e) {
        if (mIViewReference.get() != null) {
            mIViewReference.get().showErrorView();

        }
    }

    @Override
    public void onProgress(int progress) {

    }
}
